<?php
  include_once dirname(__FILE__)."/../database/database.class.php";

  class Collection
  {
    protected $id;

    private $details = null;
    
    public function Collection($id)
    {
      $this->id = $id;
    }

    public function getID()
    {
      return $this->id;
    }

    private function getDetails()
    {
      global $db;

      if($this->details == null)
      {
        $this->details = $db->getCollectionDetails($this->id);
      }
    }
    
    public function getName()
    {
      $this->getDetails();
      if(!empty($this->details->name))
      {
        return $this->details->name;
      }
      return "";
    }

    public function getDescription()
    {
      $this->getDetails();
      if(!empty($this->details->description))
      {
        return $this->details->description;
      }
      return "";
    }

    public function getVisibility()
    {
      $this->getDetails();
      if(!empty($this->details->Visibility))
      {
        return $this->details->Visibility;
      }
      return "";
    }
    
    public function setDetails($name, $description)
    {
      global $db;

      $db->changeCollection($this->id, $name, $description);
    }

    public function getCards($order = array(), $limit = array())
    {
      global $db;
      static $return_cards = array();
      static $previous_order = null;
      static $previous_limit = null;
      
      if(empty($return_cards) || $previous_order !== $order || $previous_limit !== $limit)
      {
        $previous_order = $order = array_merge(array(array('column' => "`Expansion`.`release_date`", 'direction' => "DESC")), $order);
        $previous_limit = $limit;
        $cards = $db->getCardsInCollection($this->id, $order, $limit);

        foreach($cards as $card)
        {
          array_push($return_cards, new Card($card->ID));
        }
      }

      return $return_cards;
    }
    
    public function getSize()
    {
      global $db;

      return $db->getCollectionSize($this->id);
    }

    public function getUniqueSize()
    {
      global $db;

      return $db->getTotalUniqueCardsInCollection($this->id);
    }

    public function getAmount($cardid)
    {
      global $db;

      $amount = $db->getAmountInCollection($cardid, $this->id);
      return $amount;
    }

    public function print_images($order, $limit)
    {
      global $db;

      $current_expansion = NULL;

      $cards = $this->getCards($order, $limit);
      foreach($cards as $card)
      {
        $expansion = $db->getExpansionOfCard($card->cardid);
        $amount = $this->getAmount($card->cardid);

        if($current_expansion != $expansion->name)
        {
          $current_expansion = $expansion->name;
          echo "<h2 style=\"clear:both; padding-top:20px;\">$current_expansion</h2>";
        }

        echo "<div style=\"float:left; position:relative; height:310px;\">
                ".get_image($expansion->internal_name, $card->cardid, $card->cardname)."
                <div style=\"width:100%; position:absolute; opacity:1.0; margin:0; bottom:0px; padding:0;\"><p style=\"color:white; margin:0; padding:1px; font-size:10px;\"><b>Total: $amount[0] | $amount[1]</b></p></div>
              </div>";
      }
    }
    
    public function get_mana_distribution()
    {
      $manacost = array();

      foreach($this->getCards() as $card)
      {
        if(strpos($card -> getType(), 'Land') === false)
        {
          $CMC = $card->getConvertedManaCost();
          if(!isset($manacost[$CMC]))
            $manacost[$CMC] = array_sum($this->getAmount($card->getID()));
          else
            $manacost[$CMC] += array_sum($this->getAmount($card->getID()));
        }
      }
      return $manacost;
    }
    
    public function get_color_distribution()
    {
      $manacost = array("white" => 0, "blue" => 0, "black" => 0, "red" => 0, "green" => 0, "generic" => 0);

      foreach($this->getCards() as $card)
      {
        $manaCostSymbols = $card->getManaCostSymbols();
      
        if(array_sum($manaCostSymbols) != 0)
        {
          foreach($manaCostSymbols as $symbol => $value)
          {
            $manacost[$symbol] += $value * array_sum($this->getAmount($card->getID()));
          }
        }
      }
      return $manacost;
    }
    
    public function get_type_distribution()
    {
      $types = array();
    
      foreach($this->getCards() as $card)
      {
        if(!isset($types[$card->getType()])) $types[$card->getType()] = 0;

        $types[$card->getType()] += array_sum($this->getAmount($card->getID()));
      }
      
      return $types;
    }
  }
?>
